package com.tencent.wxcloudrun.frontcontroller;

import com.alibaba.fastjson.JSONObject;
import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.partnerpayments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.partnerpayments.jsapi.model.*;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author lichongbing
 * @version 1.0.0
 * @createdate 2023/3/9 15:50
 * @description: TODO
 */
@RestController
@Api(tags = "服务商支付模块")
public class PaypartnerController {
    @Value("${wxpay.merchantId}")
    private String merchantId;
    @Value("${wxpay.privateKeyPath}")
    private String privateKeyPath;
    @Value("${wxpay.merchantSerialNumber}")
    private String merchantSerialNumber;
    @Value("${wxpay.apiV3key}")
    private String apiV3key;
    @Value("${wxpay.appid}")
    private String appid;

    public static Config config = null;

    public static JsapiServiceExtension service = null;

    /**
     * 服务商支付
     */
    @PostMapping(value = "/pay/placeOder2")
    public PrepayWithRequestPaymentResponse getString1(HttpServletRequest requests, @RequestBody JSONObject jsonObject){
        String openid = requests.getHeader("X-WX-OPENID");
        if (config == null){
            config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(merchantId)
                    .privateKeyFromPath(System.getProperty("user.dir") + privateKeyPath)
                    .merchantSerialNumber(merchantSerialNumber)
                    .apiV3Key(apiV3key)
                    .build();
        }
        if(service==null){
            service = new JsapiServiceExtension.Builder().config(config).build();
        }
        String requestPaymentAppid ="wx9f1f1f1f1f1f1f1f";
        PrepayRequest request = new PrepayRequest();
        Amount amount = new Amount();
        Integer money = jsonObject.getInteger("money");
        amount.setCurrency("CNY");
        amount.setTotal(money);
        amount.setTotal(1);
        request.setAmount(amount);
        request.setSubAppid(appid);
        request.setSubMchid(merchantId);
        request.setDescription(jsonObject.getString("remark"));
        request.setNotifyUrl("https://springboot-uvws-33830-5-1302898718.sh.run.tcloudbase.com/pay/callback1");
        String outTradeNo = getOutTradeNo();
        request.setOutTradeNo(outTradeNo);
        Payer payer = new Payer();
        payer.setSpOpenid(openid);
        request.setPayer(payer);
        PrepayWithRequestPaymentResponse prepayWithRequestPaymentResponse = service.prepayWithRequestPayment(request, requestPaymentAppid);
        System.out.println(prepayWithRequestPaymentResponse);
        return prepayWithRequestPaymentResponse;
    }
    /**
     * 生成商户订单号22位
     *
     * @return
     */
    public static String getOutTradeNo() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String date = sdf.format(new Date());
        String randomstring = RandomStringUtils.randomNumeric(8);
        return date + randomstring;
    }
    /**
     * 支付回调
     * @param request
     * @param jsonObject
     * @return
     */
    @PostMapping(value = "/pay/callback1")
    public String callback1(HttpServletRequest request, @RequestBody JSONObject jsonObject) {
        System.out.println("支付回调");
        System.out.println(jsonObject);
        return "SUCCESS";
    }

}
